#include "cm_nat/MulticastPort.h"

using namespace cm_nat;

class TestListenPort: public MulticastPortListener
{
public:

    void onReceive(MulticastPort*p_port, ByteBuffer* p_data)
    {
        std::cout<< "get a packet\n";
    }

};

int main2()
{
    SocketReactor reactor;
    reactor.setTimeout(Poco::Timespan(1,0,0,0,0));

    ByteBufferPool bpool;
    bpool.setup(10, 200);

    SocketAddress group("239.255.1.2", 12345);
    MulticastPort lp(reactor, &bpool);
    lp.setup( MulticastPort::MULTICAST_SERVER, group );

    TestListenPort tlp;
    lp.registerListener( &tlp );


    //SocketAddress stunSa("stun.l.google.com","19302");
    //SocketAddress stunSa("192.168.1.99","3478");
    SocketAddress stunSa("stunserver.org", 3478);

    Thread t;
    t.start(reactor);

    std::string a;
    std::cin>>a;


    return 0;
}


int main()
{
    SocketReactor reactor;
    reactor.setTimeout(Poco::Timespan(1,0,0,0,0));

    ByteBufferPool bpool;
    bpool.setup(10, 200);

    SocketAddress group("239.255.1.2", 12345);

    MulticastPort lp(reactor, &bpool);
    lp.setup( MulticastPort::MULTICAST_CLIENT, group );

    TestListenPort tlp;
    lp.registerListener( &tlp );

    Thread t;
    t.start(reactor);

    std::string a;
    
    while(1){
        std::cin>>a;
        ByteBuffer* p_data = bpool.get(0);
        if( p_data ){
            p_data->increaseRef();
            p_data->setLength(50);
            lp.send( p_data, group);
        }
        
    }


    return 0;
}